#ifndef WORLD_H
#define WORLD_H

#include "Entity.h"
#include "Constraint.h"
#include "Level.h"
#include "Maths.h"
#include "Physics.h"

#include <vector>

class CWorld
{
    
public:
    
    ~CWorld()
    {
        //Clean up the world
        while (m_pEntities.size()) {
            delete m_pEntities.back();
            m_pEntities.pop_back();
        }
        while (m_pConstraints.size()) {
            delete m_pConstraints.back();
            m_pConstraints.pop_back();
        }
    }
    
    void loadLevel(String sName);
    
    //Update dynamic entities
    void update(Scalar fTimeStep);
    
    //Draw all entities
    void draw();
    
    CPhysicsPoint* addPhysicsPoint(Vector vPos, Scalar fMass = 1.f, Scalar fRotInertia = 1.f);
    
    CEntity* addEntity(String sName, Vector vPos, String sTextureName, RGBAColor rgbaColor=RGBAColor(1, 1, 1, 1));
    CEntity* addCollidingEntity(CEntity entity);
    //CPlayer* addPlayer(CPlayer player);
    //TODO: Implement the following function
    CLengthConstraint*  addLengthConstraint(CPhysicsSegment segment, Scalar fLength = -1);
    CSlideConstraint*   addSlideConstraint(CPhysicsPoint* pSlidingPoint, CSegment segment);
    CSpringConstraint*  addSpringConstraint(CPhysicsSegment segment, Scalar fSpringConstant = 1.f, Scalar fDampingConstant = 0.f, Scalar fLength = -1);
    
private:
    
    void doCollisionDetection();
    
    void updateObjects(Scalar fTimeStep);
    
    CLevel m_Level;
    
    typedef std::vector<CPhysicsPoint*> PhysicsPointList;
    typedef std::vector<CPhysicsPoint*>::iterator PhysicsPointIterator;
    PhysicsPointList m_pPhysicsPoints;
    
    typedef std::vector<CEntity*> EntityList;
    typedef std::vector<CEntity*>::iterator EntityIterator;
    EntityList m_pEntities;
    EntityList m_pCollidingEntities;
    
    typedef std::vector<IConstraint*> ConstraintList;
    typedef std::vector<IConstraint*>::iterator ConstraintIterator;
    ConstraintList m_pConstraints;
    
};

#endif //WORLD_H
